import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * java对象的比较
 */
class Student implements Comparable<Student> {
    public int age;
    public String name;
    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    public boolean equals(Object object) {
        Student student = (Student) object;
        return this.name.equals(student.name);
    }

    /**
     * 重写Comparable的compareTo方法
     * @param o the object to be compared.
     * @return Student年龄差值
     */
    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}
class NameComparator implements Comparator<Student> {
    public int compare(Student student1,Student student2) {
        return student1.name.compareTo(student2.name);//String重写了compareTo方法
    }
}
public class Test2 {
    /**
     * java对象的比较
     *      1.PriorityQueue插入引用对象
     *      2.元素的比较
     *      3.引用类型对象的比较
     *      4.集合框架中PriorityQueue的比较方式
     *      5.使用PriorityQueue解决TOPK问题
     * @param args
     */
    public static void main(String[] args) {

    }
    /**
     * PriorityQueue的两种比较方式
     * @param args
     */
    public static void main7(String[] args) {
        /**
         * PriorityQueue的两种比较方式
         *      1.实现Comparable接口的compareTo方法->直接调用其重写方法
         *      2.实现Comparator接口的compare方法->传比较器
         */
        Student student1 = new Student("zhangSan",18);
        Student student2 = new Student("lisi",20);
        PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>();
        priorityQueue1.offer(student1);
        priorityQueue1.offer(student2);
        System.out.println("上面两行代码,以重写compareTo方法实现了引用类型构造为小根堆");

        Cmp cmp = new Cmp();
        PriorityQueue<Student> priorityQueue2 = new PriorityQueue<>(cmp);
        priorityQueue2.offer(student1);
        priorityQueue2.offer(student2);
        System.out.println("上面代码,以构造器形式实现了引用类型构造为大根堆");
    }
    public static void main6(String[] args) {
        /**
         * 三种方式的对比
         *      1.equals
         *      2.Comparable.compareTo
         *      3.Comparator.compare
         */
    }
    /**
     * 引用类型对象的比较
     * @param args
     *      1.重写Object的equals方法
     *      2.类实现Comparable的compareTo方法
     *      3.新类实现CompareTor的compare方法
     */
    /**
     * 新类实现CompareTor的compare方法
     * @param args
     *      1.创建新类,重写方法
     *      2.实例化该类
     *      3.使用该类对象调用compare方法比较即可
     */
    public static void main5(String[] args) {
        Student student1 = new Student("zhangSan",18);
        Student student2 = new Student("lisi",20);
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1,student2));
    }
    /**
     * 类实现Comparable的compareTo方法
     * @param args
     *      1.实现Comparable类
     *      2.重写compareTo方法
     *      3.类调用compareTo方法
     */
    public static void main4(String[] args) {
        Student student1 = new Student("zhangSan",18);
        Student student2 = new Student("zhangSan",20);
        System.out.println(student1.compareTo(student2));
    }
    /**
     * 重写Object的equals方法->
     * @param args
     */
    public static void main3(String[] args) {
        Student student1 = new Student("zhangSan",18);
        Student student2 = new Student("zhangSan",18);
        System.out.println(student1.equals(student2));
    }
    public static void main2(String[] args) {
        /**
         * 元素的比较
         *      1.基本数据类型比较
         *          可以直接进行比较
         *      2.引用类型对象比较
         *          可以比较,但是有问题
         */
        int a = 10;
        int b = 20;
        System.out.println(a > b);

        Student student1 = new Student("zhangSan",18);
        Student student2 = new Student("zhangSan",18);
        System.out.println(student1 == student2);//false
        //直接进行比较会继承Object的equals类,且默认根据其地址进行比较,
        // 所以即使name + age相同,结果仍然为false
    }

    public static void main1(String[] args) {
        /**
         * PriorityQueue中插入引用类型对象
         * @param args
         *      不做处理无法插入,因为引用类型不能直接进行比较
         */
    }
}
